package com.example.a66666.yuejian.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a66666.yuejian.Bean.GameBean;
import com.example.a66666.yuejian.Bean.LoginBean;
import com.example.a66666.yuejian.Listener.RetrofitListener;
import com.example.a66666.yuejian.Model.UserModel;
import com.example.a66666.yuejian.R;
import com.squareup.picasso.Picasso;

import org.w3c.dom.Text;

public class GameDetailActivity extends AppCompatActivity {
    private TextView gameName,gameTime,gamePlace,gameNum,gameIntro,goBack,collect;
    private ImageView imageView;
    private UserModel userModel,isCollectModel,collectModel,cancelCollectModel;
    private RetrofitListener<GameBean> listener;
    private RetrofitListener<LoginBean> isCollectListener,collectListener,cancelCollectListener;
    private SharedPreferences sp;
    private int gameID;
    private int uid;
    private int isCollected=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_game_detail);
        sp=getSharedPreferences("me", MODE_PRIVATE);
        uid=sp.getInt("userid",6666);
        initView();
        initListener();
        initClick();
        getData();
    }

    public void initView(){
        gameName=(TextView)findViewById(R.id.name);
        gameTime=(TextView)findViewById(R.id.time);
        gamePlace=(TextView)findViewById(R.id.place);
        gameNum=(TextView)findViewById(R.id.num);
        gameIntro=(TextView)findViewById(R.id.intro);
        goBack=(TextView)findViewById(R.id.goback);
        collect=(TextView)findViewById(R.id.collect);
        imageView=(ImageView)findViewById(R.id.game_picture);
    }

    public void initListener(){
        listener=new RetrofitListener<GameBean>() {
            @Override
            public void onSuccess(GameBean gameBean) {
                gameID=gameBean.getGid();
                gameName.setText(gameBean.getGname());
                gameTime.setText(gameBean.getGtime());
                gamePlace.setText(gameBean.getGaddress());
                gameNum.setText(gameBean.getGnum()+"人");
                gameIntro.setText(gameBean.getGintro());
                Picasso.with(imageView.getContext()).load("http://10.0.2.2/yuejian/public/static/gamepic/"+gameBean.getGpic()).into(imageView);
                isCollectModel=new UserModel();
                isCollectModel.isCollectGame(uid,gameID,isCollectListener);
            }

            @Override
            public void onFail(String msg) {

            }
        };

        isCollectListener=new RetrofitListener<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean) {
                if(loginBean.getFlag()==1){
                    collect.setText("取消关注");
                    isCollected=1;
                }
                else{
                    collect.setText("关注");
                    isCollected=0;
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };

        cancelCollectListener=new RetrofitListener<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean) {
               if(loginBean.getFlag()==1){
                   isCollected=0;
                   Toast.makeText(GameDetailActivity.this,"取消关注成功",Toast.LENGTH_LONG).show();
                   collect.setText("关注");
               }
            }

            @Override
            public void onFail(String msg) {

            }
        };

        collectListener=new RetrofitListener<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean) {
                if(loginBean.getFlag()==1){
                    isCollected=1;
                    Toast.makeText(GameDetailActivity.this,"关注成功",Toast.LENGTH_LONG).show();
                    collect.setText("取消关注");
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };
    }

    public void getData(){
        userModel=new UserModel();
        userModel.getGameDetail(getIntent().getIntExtra("gameID",1),listener);
    }
    public void initClick(){
        goBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(GameDetailActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });

        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isCollected==0){
                    collectModel=new UserModel();
                    collectModel.collectGame(uid,gameID,collectListener);
                }
                else{
                    cancelCollectModel=new UserModel();
                    cancelCollectModel.cancelCollectGame(uid,gameID,cancelCollectListener);
                }
            }
        });

    }
}
